package it.unica.informatica.enotes.notes;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Parcel;
import android.os.Parcelable;

import org.json.JSONObject;

import java.io.File;

/**
 * This class manages a generic attachment contained inside a note
 * @author Mirko Marras
 * @version 1.0
 */
public class AttachmentItem implements Parcelable {

    public static final String TYPE_IMAGE = "image/*";
    public static final String TYPE_VIDEO = "video/*";
    public static final String TYPE_AUDIO = "audio/*";

    private String content;
    private String name;
    private String type;
    private Bitmap thumbnails;

    /**
     * Construct a new attachment with default values
     */
    public AttachmentItem() {
        this.content = "";
        this.name = "";
        this.type = "";
        this.thumbnails = null;
    }

    /**
     * Construct a new attachment based on the values passed as parameters
     * @param content The BASE64 string that encodes the attachment content
     * @param name The name of the attachment
     * @param type The type of the attachment
     * @param thumbnails The image used to show a preview of the attachment
     */
    public AttachmentItem(String content, String name, String type, Bitmap thumbnails) {
        this.content = content;
        this.name = name;
        this.type = type;
        this.thumbnails = thumbnails;
    }

    /**
     * Get the name of this attachment object
     * @return The name of the attachment object
     */
    public String getName() {
        return this.name;
    }

    /**
     * Set a new name for this attachment object
     * @param name The new name for the attachment object
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Get the type of this attachment object
     * @return The type of the attachment object
     */
    public String getType() {
        return this.type;
    }

    /**
     * Set a new type for this attachment object
     * @param type The new type for the attachment object
     */
    public void setType(String type) {
                this.type = type;
    }

    /**
     * Get the BASE64 string that contains the content of this attachment object
     * @return The BASE64 string that contains the content of the attachment object
     */
    public String getContent() {
        return this.content;
    }

    /**
     * Set a BASE64 string that contains the new content for this attachment object
     * @param content The BASE64 string that contains the new content for the attachment object
     */
    public void setContent(String content) {
        this.content = content;
    }

    /**
     * Get the preview image of this attachment object
     * @return The preview image of the attachment object
     */
    public Bitmap getThumbnails() {
        return this.thumbnails;
    }

    /**
     * Set a new preview image for this attachment object
     * @param thumbnails The new preview image for the attachment object
     */
    public void setThumbnails(Bitmap thumbnails) {
        this.thumbnails = thumbnails;
    }

    /**
     * Check if the attachment object is empty
     * @return True if the attachment object is empty, false otherwise
     */
    public boolean isEmpty() {
        return  this.content.equals("") &&
                this.name.equals("") &&
                this.type.equals("") &&
                this.thumbnails == null;
    }

    @Override
    public String toString() {
        return "AttachmentItem{" +
                "content='" + content + '\'' +
                ", name='" + name + '\'' +
                ", type='" + type + '\'' +
                ", thumbnails=" + thumbnails +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof AttachmentItem)) return false;

        AttachmentItem that = (AttachmentItem) o;

        if (!content.equals(that.content)) return false;
        if (!name.equals(that.name)) return false;
        if (!thumbnails.equals(that.thumbnails)) return false;
        if (!type.equals(that.type)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = content.hashCode();
        result = 31 * result + name.hashCode();
        result = 31 * result + type.hashCode();
        result = 31 * result + thumbnails.hashCode();
        return result;
    }

    /**********************************************************************************************
     *                                        PARCELABLE METHODS                                  *
     **********************************************************************************************/

    public static final Parcelable.Creator<AttachmentItem> CREATOR
            = new Parcelable.Creator<AttachmentItem>() {
        public AttachmentItem createFromParcel(Parcel in) {
            return new AttachmentItem(in);
        }

        public AttachmentItem[] newArray(int size) {
            return new AttachmentItem[size];
        }
    };

    /**
     * Describe the content of the parcelable
     * @return Always zero
     */
    public int describeContents() {
        return 0;
    }

    /**
     * Save an Attachment in the parcelable
     * @param out The output parcelable
     * @param flags Flags used to save
     */
    public void writeToParcel(Parcel out, int flags) {
        out.writeString(content);
        out.writeString(name);
        out.writeString(type);
        out.writeParcelable(thumbnails, flags);
    }

    /**
     * Recreate an Attachment from the parcelable
     * @param in The parcelable where read
     */
    private AttachmentItem(Parcel in) {
        this.content = in.readString();
        this.name = in.readString();
        this.type = in.readString();
        this.thumbnails = in.readParcelable(Bitmap.class.getClassLoader());
    }

}
